package com.example.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.entity.Work;

import com.example.mapper.WorkMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 课程信息业务层处理
 */
@Service
public class WorkService {
    @Resource
    private WorkMapper workMapper;

    public void add(Work work) {
        work.setState("待提交");
        workMapper.insert(work);
    }

    public PageInfo<Work> selectPage(Work work, Integer pageNum, Integer pageSize) {
        List<Work> list;
        PageHelper.startPage(pageNum, pageSize);
        if(ObjectUtil.isNotEmpty(work.getStudentId())) {
            //说明是学生登录进行分页查询
            if(ObjectUtil.isNotEmpty(work.getName())) {
                list = workMapper.selectByNameAndStudentId(work.getName(),work.getStudentId());
            }
            else{
                list = workMapper.selectAllByStudentId(work.getStudentId());
            }
        } else if(ObjectUtil.isNotEmpty(work.getTeacherId())){
            //说明是老师登录进行分页查询
            if(ObjectUtil.isNotEmpty(work.getName())) {
                list = workMapper.selectByNameAndTeacherId(work.getName(),work.getTeacherId());
            }
            else{
                list = workMapper.selectAllByTeacherId(work.getTeacherId());
            }
        }
        else {//说明是管理员登录进行分页查询
            if(ObjectUtil.isNotEmpty(work.getName())) {
                list = workMapper.selectByName(work.getName());
            }
            else{
                list =workMapper.selectAll();
            }
        }
        return PageInfo.of(list);
    }

    public void updateByID(Work work) {
        if(ObjectUtil.isNotEmpty(work.getStudentId())){
            work.setState("待审核");
        }
        workMapper.updateById(work);
    }

    public void deleteById(Integer id) {
        workMapper.deleteById(id);
    }


    public List<Work> selectAll() {
        return workMapper.selectAll();
    }
}
